//
//  AY_BookMyPage.m
//  AiYue
//
//  Created by boWen on 2023/10/19.
//

#import "AY_BookMyPage.h"
#import "AY_BookMyHeaderView.h"
#import "AY_BookMyCell.h"
#import "AY_BookCenterVpCell.h"
#import "AY_BookVpPage.h"
#import "AY_BookQiuBookPage.h"
#import "AY_BookInviteAlertView.h"
#import "AY_BookOpinionPage.h"
#import "AY_GenderViewController.h"

@interface AY_BookMyPage ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * baseTableView;
@property (nonatomic, strong) AY_BookMyHeaderView * myHeaderView;
@property (nonatomic, strong) AY_BookInviteAlertView * inviteView;
@property (nonatomic, strong) UserInfo * userInfoModel;
@property (nonatomic, strong) NSMutableArray * dataAry;
@property (nonatomic, strong) ConfigModel *configModel;

@end

@implementation AY_BookMyPage

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getConfigData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithNav];
}

- (void)initWithNav {
    self.gk_navigationBar.hidden = YES;
    [self.view addSubview:self.baseTableView];
}

- (void)getConfigData {
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_enter(group);
    [AYApiRequest getUserInfoSuccess:^(AY_UserModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            self.userInfoModel = pkg.data;
            self.myHeaderView.infoModel = self.userInfoModel;
        }
        dispatch_group_leave(group);
    } failure:^(int status, NSString * _Nonnull info) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    [AYApiRequest APPConfigSuccess:^(AY_ConfigModel * _Nonnull pkg) {
        self.configModel = pkg.data;
        self.myHeaderView.configModel = self.configModel;
        dispatch_group_leave(group);
    } failure:^(int status, NSString * _Nonnull info) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [self.baseTableView reloadData];
    });
}

- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectMake(0, 0, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT  - GK_TABBAR_HEIGHT)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.layer.masksToBounds = YES;
        myTableView.tableHeaderView = self.myHeaderView;
        [myTableView registerClass:[AY_BookMyCell class] forCellReuseIdentifier:@"AY_BookMyCell"];
        [myTableView registerClass:[AY_BookCenterVpCell class] forCellReuseIdentifier:@"AY_BookVipCell"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}

- (AY_BookMyHeaderView *)myHeaderView {
    if(!_myHeaderView) {
        if ([AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
            AY_BookMyHeaderView * myView = [[AY_BookMyHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, YNUM(75) + GK_STATUSBAR_NAVBAR_HEIGHT)];
            _myHeaderView = myView;
        }else{
            AY_BookMyHeaderView * myView = [[AY_BookMyHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, YNUM(135) + GK_STATUSBAR_NAVBAR_HEIGHT)];
            _myHeaderView = myView;
        }
    }
    return _myHeaderView;
}

- (AY_BookInviteAlertView *)inviteView {
    if(!_inviteView) {
        _inviteView = [[AY_BookInviteAlertView alloc] initWithFrame:CGRectMake(0, 0, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT)];
        Weak_Self
        _inviteView.inviteBlock = ^{
            [weakSelf inviteCodeUpdate];
        };
    }
    return _inviteView;
}

- (void)inviteCodeUpdate {
    [AYApiRequest postUserInfoUpdate:self.inviteView.inviteField.text param:@"invitedByCode" updateType:@"5" success:^(AY_UserUpdateModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            ShowToast(@"邀请成功");
            [self.inviteView dismissPopView];
            [self getConfigData];
        }else {
            ShowToast(pkg.msg);
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

- (NSMutableArray *)dataAry {
    if(!_dataAry) {
        if ([AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
            _dataAry = [NSMutableArray arrayWithArray:@[[AYMMKV getInt32ForKey:GENDER defaultValue:1] == 1 ? @"男生频道" : @"女生频道", @"已帮999位书友找到书", @"在线时间9:00-21:00", @""]];
        }else{
            _dataAry = [NSMutableArray arrayWithArray:@[[AYMMKV getStringForKey:INVITEFIREND], [AYMMKV getInt32ForKey:GENDER defaultValue:1] == 1 ? @"男生频道" : @"女生频道", @"已帮999位书友找到书", @"在线时间9:00-21:00", @""]];
        }
    }
    return _dataAry;
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
        return self.dataAry.count;
    }else{
        return section == 0 ? 1 : self.dataAry.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
        AY_BookMyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookMyCell"];
        cell.dataRightAry = self.dataAry;
        [cell setData:indexPath.row andConfigModel:self.configModel];
        cell.infoModel = self.userInfoModel;
        return cell;
    }else{
        if(indexPath.section == 0) {
            AY_BookCenterVpCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookVipCell"];
            cell.infoModel = self.userInfoModel;
            return cell;
        }else {
            AY_BookMyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookMyCell"];
            cell.dataRightAry = self.dataAry;
            [cell setData:indexPath.row andConfigModel:self.configModel];
            cell.infoModel = self.userInfoModel;
            return cell;
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
        return 1;
    }else{
        return 2;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
        return YNUM(52);
    }else{
        return indexPath.section == 0 ? YNUM(116) : YNUM(52);
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return YNUM(12);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BOOL isShenHe = [AYMMKV getBoolForKey:ISSHENHE defaultValue:YES];
    if (isShenHe) {
        if(indexPath.row == 0) {
            AY_GenderViewController *genderVC = [[AY_GenderViewController alloc] init];
            genderVC.pageType = @"mine";
            [self.navigationController pushViewController:genderVC animated:YES];
        }
        if(indexPath.row == 1) {
            AY_BookQiuBookPage * page = [[AY_BookQiuBookPage alloc] init];
            [self.navigationController pushViewController:page animated:YES];
        }
        if (indexPath.row == 2) {
            AY_WebViewController *webVC = [[AY_WebViewController alloc] init];
            webVC.fullPath = [NSString stringWithFormat:@"%@%@",self.configModel.web_url,self.configModel.kfAdress];
            [self.navigationController pushViewController:webVC animated:YES];
        }

        if(indexPath.row == 3) {
            AY_BookOpinionPage * page = [[AY_BookOpinionPage alloc] init];
            [self.navigationController pushViewController:page animated:YES];
        }
    }else{
        if(indexPath.section == 0 ) {
            AY_BookVpPage * page = [[AY_BookVpPage alloc] init];
            [self.navigationController pushViewController:page animated:YES];
        }
        
        if (indexPath.section == 1) {
//            if(indexPath.row == 0) {
//                [self.inviteView showPopView];
//            }
            
            if(indexPath.row == 1) {
                AY_GenderViewController *genderVC = [[AY_GenderViewController alloc] init];
                genderVC.pageType = @"mine";
                [self.navigationController pushViewController:genderVC animated:YES];
            }
            if(indexPath.row == 2) {
                AY_BookQiuBookPage * page = [[AY_BookQiuBookPage alloc] init];
                [self.navigationController pushViewController:page animated:YES];
            }
            if(indexPath.row == 4) {
                AY_BookOpinionPage * page = [[AY_BookOpinionPage alloc] init];
                [self.navigationController pushViewController:page animated:YES];
            }
            if (indexPath.row == 3) {
                AY_WebViewController *webVC = [[AY_WebViewController alloc] init];
                webVC.fullPath = [NSString stringWithFormat:@"%@%@",self.configModel.web_url,self.configModel.kfAdress];
                [self.navigationController pushViewController:webVC animated:YES];
            }
            if (indexPath.row == 0) {
                AY_WebViewController *webVC = [[AY_WebViewController alloc] init];
                webVC.fullPath = [NSString stringWithFormat:@"%@%@",self.configModel.web_url,self.configModel.invite_friend];
                [self.navigationController pushViewController:webVC animated:YES];
            }
        }

    }
}
@end
